<cfcomponent output="false">
	
	<!--- TOKEN FUNCTIONS --->
	
	<cffunction name="getRefreshToken" access="public" output="false" returntype="struct">
		<cfargument name="code" required="true" />
		
		<cfset var local = {} />

		<cfset local.result = $.run.http(
			url="https://accounts.google.com/o/oauth2/token",
			args={
				code=arguments.code,
				scope="https://www.googleapis.com/auth/calendar",
				client_id=$.site.getVar('google_email_address'),
				client_secret=$.site.getVar('google_client_secret'),
				redirect_uri=$.site.getVar('google_redirect_uri'),
				grant_type="authorization_code"
			},
			message="getRefreshToken"
		) />
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="requestAccessToken" access="public" output="false" returntype="struct">
		<cfargument name="refresh_token" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.result = $.run.http(
			url="https://accounts.google.com/o/oauth2/token",
			args={
				client_id=$.site.getVar('google_client_id'),
				client_secret=$.site.getVar('google_client_secret'),
				refresh_token=arguments.refresh_token,
				grant_type="refresh_token"
			},
			message="getAccessToken"
		) />
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="revokeRefreshToken" access="public" output="false" returntype="struct">
		<cfargument name="refresh_token" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.result = $.run.http(
			url="https://accounts.google.com/o/oauth2/revoke",
			args={token=arguments.refresh_token},
			message="revokeRefreshToken",
			success_codes="200,400"
		) />
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="deleteCalendar" access="public" output="false" returntype="struct">
		<cfargument name="googleCalendarID" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/calendars/#arguments.googleCalendarID#",
			args={},
			type='DELETE',
			authorization=local.access_token,
			message="deleteCalendar",
			success_codes="200,204,404,410"
		) />
		<!--- 404 calendar already deleted --->
		<!--- 204 calendar successfully deleted --->
		<!--- 410 already gone --->
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="createCalendar" access="public" output="false" returntype="struct">
		<cfargument name="summary" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/calendars",
			args={
				summary=arguments.summary,
				access_token=local.access_token
			},
			type='POST',
			authorization=local.access_token,
			contentType='application/json',
			message="createCalendar"
		) />

		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getCalendarList" access="public" output="false" returntype="struct">
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/users/me/calendarList",
			type='GET',
			authorization=local.access_token,
			message="getCalendarList"
		) />

		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getEvents" access="public" output="false" returntype="struct">
		<cfargument name="calendarID" required="true" />
		<cfargument name="args" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<!--- limit events to 2013 and forward --->
		<!--- <cfset arguments.args.timeMin = "2011-01-01T00:00:00.000+00:00"/> --->
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/calendars/#arguments.calendarID#/events",
			args=arguments.args,
			type='GET',
			authorization=local.access_token,
			message="getEvents",
			success_codes="200,410"
		) />
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="deleteEvent" access="public" output="false" returntype="struct">
		<cfargument name="calendarID" required="true" />
		<cfargument name="eventID" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/calendars/#arguments.calendarID#/events/#arguments.eventID#",
			args={},
			type='DELETE',
			authorization=local.access_token,
			contentType="",
			message="deleteEvent",
			success_codes="200,204,410"
		) />
		
		<!--- 410 already gone --->
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="updateEvent" access="public" output="false" returntype="struct">
		<cfargument name="calendarID" required="true" />
		<cfargument name="eventID" required="true" />
		<cfargument name="args" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/calendars/#arguments.calendarID#/events/#arguments.eventID#",
			args=arguments.args,
			type='PUT',
			authorization=local.access_token,
			contentType="application/json",
			message="updateEvent"
		) />
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getEvent" access="public" output="false" returntype="struct">
		<cfargument name="calendarID" required="true" />
		<cfargument name="eventID" required="true" />
		<cfargument name="args" required="false" default="" />
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/calendars/#arguments.calendarID#/events/#arguments.eventID#",
			args=arguments.args,
			type='GET',
			authorization=local.access_token,
			contentType="application/json",
			message="getEvent"
		) />
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="insertEvent" access="public" output="false" returntype="struct">
		<cfargument name="calendarID" required="true" />
		<cfargument name="args" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/calendars/#arguments.calendarID#/events",
			args=arguments.args,
			type='POST',
			authorization=local.access_token,
			contentType="application/json",
			message="insertEvent"
		) />
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getEventInstances" access="public" output="false" returntype="struct">
		<cfargument name="calendarID" required="true" />
		<cfargument name="eventID" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.access_token = $.run.it('googleService.getAccessToken') />
		
		<cfset local.result = $.run.http(
			url="https://www.googleapis.com/calendar/v3/calendars/#arguments.calendarID#/events/#arguments.eventID#/instances",
			type='GET',
			args={maxResults="450"},
			authorization=local.access_token,
			message="getEventInstances"
		) />
				
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->

</cfcomponent>